986D - Perfect Encoding - CodeForces Solution


fft math *3100

Please click on ads to support us..

Python Code:

import decimal
from math import ceil, floor, inf, log

n = input()
if n=='1':
    print(1)
    exit()
decimal.getcontext().prec = len(n)+6
decimal.getcontext().Emax = len(n)+6
log3 = log(10)*(len(n)-1)/log(3)
pref = n
if len(pref)>20:
    pref = pref[:20]
pref = pref[0] + '.' + pref[1:]
log3 += log(float(pref))/log(3)
log3+=1e-8
full = max(0, floor(log3))
small=0

if full>=1 and log3-full<=log(2)/log(3)*2-1:
    small=2
    full-=1
elif log3-full<=log(2)/log(3):
    small = 1

else:
    full+=1


n = decimal.Decimal(n)
ans = full*3 + small*2

def check(f,s):
    global ans
    res = decimal.Decimal(3)**f * (2**s)
    if res>=n:
        ans = min(ans,f*3+s*2)

if small==0 and full>=1:
    full-=1
    small+=1
    check(full,small)
elif small==1 and full>=1:
    full-=1
    small+=1
    check(full,small)
elif small==2:
    small-=2
    full+=1
    check(full,small)

print(ans)

C++ Code:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;

#define rep(i,a,b) for (int i=(a); i<=(b); ++i)
#define per(i,a,b) for (int i=(a); i>=(b); --i)

const int N = 1500005, oo = 0x3f3f3f3f;
const double eps = 1e-8;
int pp[] = {0, 100030001, 100060001, 113111311, 114535411}, delta = 1000000;
char st[N]; 
int n, ans = oo;

int pow(int x, int k, int p) {
    int res = 1;
    for ( ; k; k >>= 1, x = (ll)x * x % p) {
        if (k & 1) {
            res = (ll)res * x % p;
        }
    }
    return res;
}

struct BigInt {

    vi v;

    BigInt(char st[], int n) {
        v.resize(n);
        rep(i, 0, n-1) {
            v[i] = st[n-1-i] - '0';
        }
    }
    
    inline void detrail() {
        while (!v.empty() && !v.back()) v.pop_back();
    }

    inline void comein() {
        v.push_back(0);
        for (int i = 0; i + 1 < (int)v.size(); ++i) {
            v[i+1] += v[i] / 10;
            v[i] %= 10;
        }
        detrail();
    }

    inline void add_one() { 
        v[0]++;
        comein(); 
    }

    inline int hash(int p) {
        ll x = 0; 
        per(i, (int)v.size() - 1, 0) {
            x = x * 10 + v[i];
            x %= p;
        } 
        return x;
    }

    inline void div_2() {
        if (v[0] & 1) add_one();
        v[0] >>= 1;
        for (int i = 0; i + 1 < (int)v.size(); ++i) {
            v[i] += (v[i+1] & 1) * 5;
            v[i+1] >>= 1;
        }
        detrail();
    }

    inline int log_3() {
        int l = min(15, (int)v.size()); 
        ll x = 0;
        per(i, (int)v.size() - 1, (int)v.size() - l) {
            x = x * 10 + v[i];
        }
        double w = (log(x) + ((int)v.size() - l) * log(10)) / log(3);
        int t = w, cnt = 0; 
        t += (t + 1 - w < eps);
        rep(i, 1, 4) {
            int xx = pow(3, t, pp[i]), yy = hash(pp[i]);
            cnt += (xx - yy > 0 && xx - yy <= delta);
        }
        if (cnt == 4) --t;
        rep(i, 1, 4) {
            if (pow(3, t, pp[i]) != hash(pp[i])) {
                return t + 1;
            }
        }
        return t;
    }
};

int main() {
    scanf("%s", st);
    n = strlen(st);
    if (n == 1 && st[0] == '1') {
        printf("1\n");
        return 0;
    }
    BigInt x = BigInt(st, n);
    ans = x.log_3() * 3;
    rep(i, 1, 2) {
        x.div_2();
        ans = min(ans, x.log_3() * 3 + i * 2);
    }
    printf("%d\n",ans);

    return 0;
}/*1695734992.273435*/


Comments

Submit
0 Comments
More Questions

628. Maximum Product of Three Numbers
1526A - Mean Inequality
1526B - I Hate 1111
1881. Maximum Value after Insertion
237. Delete Node in a Linked List
27. Remove Element
39. Combination Sum
378. Kth Smallest Element in a Sorted Matrix
162. Find Peak Element
1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name
1051. Height Checker
695. Max Area of Island
402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins
752. Open the Lock
1535A - Fair Playoff
1538F - Interesting Function
1920. Build Array from Permutation
494. Target Sum
797. All Paths From Source to Target
1547B - Alphabetical Strings
1550A - Find The Array
118B - Present from Lena